package cn.iocoder.yudao.module.enterprise.service.reportdownload;

import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.enterprise.controller.admin.reportdownload.vo.*;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.reportdownload.ReportDownloadDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.enterprise.convert.reportdownload.ReportDownloadConvert;
import cn.iocoder.yudao.module.enterprise.dal.mysql.reportdownload.ReportDownloadMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.*;

/**
 * 报告下载 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ReportDownloadServiceImpl implements ReportDownloadService {

    @Resource
    private ReportDownloadMapper reportDownloadMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createReportDownload(ReportDownloadCreateReqVO createReqVO) {
        // 插入
        ReportDownloadDO reportDownload = ReportDownloadConvert.INSTANCE.convert(createReqVO);
        reportDownloadMapper.insert(reportDownload);
        // 返回
        return reportDownload.getId();
    }

    @Override
    public void updateReportDownload(ReportDownloadUpdateReqVO updateReqVO) {
        // 校验存在
        validateReportDownloadExists(updateReqVO.getId());
        // 更新
        ReportDownloadDO updateObj = ReportDownloadConvert.INSTANCE.convert(updateReqVO);
        reportDownloadMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportDownload(Long id) {
        // 校验存在
        validateReportDownloadExists(id);
        // 删除
        reportDownloadMapper.deleteById(id);
    }

    private void validateReportDownloadExists(Long id) {
        if (reportDownloadMapper.selectById(id) == null) {
            throw exception(REPORT_DOWNLOAD_NOT_EXISTS);
        }
    }

    @Override
    public ReportDownloadDO getReportDownload(Long id) {
        return reportDownloadMapper.selectById(id);
    }

    @Override
    public List<ReportDownloadDO> getReportDownloadList(Collection<Long> ids) {
        return reportDownloadMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReportDownloadDO> getReportDownloadPage(ReportDownloadPageReqVO pageReqVO) {
        PageResult<ReportDownloadDO> pageResult = reportDownloadMapper.selectPage(pageReqVO);
        // 遍历，填充创建人
        pageResult.getList().forEach(reportDownload -> {
            AdminUserRespDTO userRespDTO = adminUserApi.getUser(Long.valueOf(reportDownload.getCreator())).getCheckedData();
            reportDownload.setCreator(userRespDTO.getNickname());
        });
        return pageResult;
    }

}
